package com.example.demo.leetcode100题;

import com.example.demo.StopWatchTemplate;

/**
 * @author：zzc
 * @date: 2022/9/1
 */
public class 相交链表 {

    //给你两个单链表的头节点 headA 和 headB ，请你找出并返回两个单链表相
    //交的起始节点。如果两个链表没有交点，返回 null 。

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        node1.next = new ListNode(2);
        node1.next.next = new ListNode(4);
        ListNode node2 = new ListNode(1);
        node2.next = new ListNode(3);
        node2.next.next = new ListNode(4);
        System.out.println(StopWatchTemplate.doSupplier(() -> getIntersectionNode(node1,node2)));
    }

    private static ListNode getIntersectionNode(ListNode l1, ListNode l2) {
        if (l1 == null || l2 == null) {
            return null;
        }
        ListNode pA = l1, pB = l2;
        while (pA!=pB) {
            pA = pA == null ? l2 : pA.next;
            pB = pB == null ? l1 : pB.next;
        }

        return pA;
    }
}
